package com.xhwl.centiotdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.dto.cent.iotdata.repair.RepairActionDTO;
import com.xhwl.common.enums.bi.TimeGrainEnum;
import com.xhwl.common.enums.iot.RepairStepResultEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.iot.RepairDetailLog;
import com.xhwl.centiotdata.service.RepairDetailLogService;
import com.xhwl.centiotdata.dao.RepairDetailLogMapper;
import com.xhwl.common.utils.CalendarUtil;
import com.xhwl.common.utils.StringUtils;
import org.bouncycastle.jcajce.provider.symmetric.IDEA;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Copyright (C),1998-2022, 兴海物联
 * FileName: RepairDetailLogService
 * Author: XuChang
 * Date: 2022/5/27 11:02
 * Description: 操作详情日志
 * History:
 */
@Service
public class RepairDetailLogServiceImpl extends ServiceImpl<RepairDetailLogMapper, RepairDetailLog>
        implements RepairDetailLogService {

    @Override
    public List<Date> generateDetailLog(RepairActionDTO repairActionDTO, int repairId) {
        List<Date> dateList;
        try {
            dateList = CalendarUtil.getDateList(repairActionDTO.getStartDate(), repairActionDTO.getEndDate(), RepairActionDTO.getDefaultDatePattern(), TimeGrainEnum.DAY);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        dateList.stream().forEach(date -> {
            RepairDetailLog repairDetailLog = new RepairDetailLog();
            repairDetailLog.setRepairDate(date);
            repairDetailLog.setOperateId(repairId);
            repairDetailLog.setOperateResult(RepairStepResultEnum.IDLE.getType());
            getBaseMapper().insert(repairDetailLog);
        });
        return dateList;
    }

    @Override
    public List<RepairDetailLog> getOperateDetail(int operateId) {
        return getBaseMapper().selectList(new LambdaQueryWrapper<RepairDetailLog>().eq(RepairDetailLog::getOperateId, operateId));
    }

    @Override
    public boolean updateStatus(int operateId, Date date, RepairStepResultEnum repairStepResultEnum, String resultLog, String operateLog) {
        if (repairStepResultEnum == RepairStepResultEnum.IDLE) {
            throw new BusinessException("该方法不支持的状态修改类型");
        }
        RepairDetailLog one = getOne(new LambdaQueryWrapper<RepairDetailLog>().eq(RepairDetailLog::getOperateId, operateId)
                .eq(RepairDetailLog::getRepairDate, date));
        if (one != null) {
            switch (repairStepResultEnum) {
                case FAILED:
                    if (StringUtils.isNotEmpty(resultLog)) {
                        one.setRequestLog(resultLog);
                    }
                    if (StringUtils.isNotEmpty(operateLog)) {
                        one.setOperateLog(operateLog);
                    }
                    one.setOperateEndTime(new Date());
                    one.setFailedType(0);
                    break;
                case SUCCESS:
                    one.setOperateEndTime(new Date());
                    break;
                case INVOKING:
                    one.setOperateStartTime(new Date());
                    break;
            }
            one.setOperateResult(repairStepResultEnum.getType());
            return updateById(one);
        }
        return false;
    }
}




